package activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jaden.washer.*;
import com.example.jaden.washer.MyApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;

import student.Student;

public class PersonalinfoPage extends AppCompatActivity implements View.OnClickListener{

    ImageView portrait;
    TextView username;
    TextView phonenumber;
    Button aboutus;
    Button orderinfo;
    Button infomodification;
    Button logout;
    Button shiftaccount;
    TextView balance;
    MyApplication application;
    Student student;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personalinfo_page);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);

        //获取Toolbar的实例
        Toolbar toolbar=(Toolbar)findViewById(R.id.homepage_toolbar);
        setSupportActionBar(toolbar);

        //获取控件实例
        getInstance();
        //执行按键点击监听
        setClickListener();
        //显示个人信息
        initPersonInformation();

}

    private void initPersonInformation()
    {
        /*
        String url="http://47.96.159.236:8080/Test/Citizen?method=getCitizenInfor";
        //打包Json数据
        JSONObject userJson=new JSONObject();
        try {

            //userJson.put("citizenUsername",application.getCitizen().getCitizenUsername());
            userJson.put("citizenID",application.getStudent().getStuID());
            String messageJson=String.valueOf(userJson);
            TransferData.GetCitizenInfo(url,messageJson);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        while (true) {
            if(application.isGetCitizenInfoOk()) {
                application.setGetCitizenInfoOk(false);
                ResponseMessage responseMessage=application.getResponseMessage();
                verification(responseMessage);

                break;
            }
        }
*/
        student =application.getStudent();
        /**
         * 用户名 电话号码 余额的显示
         */

        username.setText("用户ID:"+ student.getStuID());
        phonenumber.setText("手机号"+ student.getStuPhone());
        balance.setText(balance.getText()+String.valueOf(student.getStuMoney())+"￥");

    }

    public void verification(ResponseMessage responseMessage)
    {
        switch (responseMessage.getCode()) {
            case 1060:
                Student student = (Student) responseMessage.getData();
                application.setStudent(student);
                break;
            case 110:
                Toast.makeText(this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
            case 1061:
                Toast.makeText(this,responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
                default:
                    break;
        }
    }
    //获取控件实例
    private void getInstance() {
        portrait=(ImageView)findViewById(R.id.imageView_portrait_personnalinfo_page_resident);
        username=(TextView)findViewById(R.id.textView_username_personnalinfo_page_resident);
        phonenumber=(TextView)findViewById(R.id.textView_phonenumber_personnalinfo_page_resident);
        aboutus=(Button)findViewById(R.id.button_aboutus_personnalinfo_page_resident);
        orderinfo=(Button)findViewById(R.id.button_order_personnalinfo_page_resident);
        infomodification=(Button)findViewById(R.id.button_modifyinfo_personnalinfo_page_resident);
        logout=(Button)findViewById(R.id.button_logout_personnalinfo_page_resident);
        shiftaccount=(Button)findViewById(R.id.button_shiftaccount_personnalinfo_page_resident);
        balance=(TextView)findViewById(R.id.textView_balance_personnalinfo_page_resident);
        application=MyApplication.getInstance();
    }
    //执行按键点击监听
    private void setClickListener() {
        aboutus.setOnClickListener(this);
        logout.setOnClickListener(this);
        orderinfo.setOnClickListener(this);
        infomodification.setOnClickListener(this);
        shiftaccount.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId())
        {
            case R.id.button_aboutus_personnalinfo_page_resident:
                AlertDialog.Builder dialog=new AlertDialog.Builder(PersonalinfoPage.this);
                dialog.setTitle("资源回收平台");
                String message="";
                dialog.setMessage(message);
                //能否用back键关闭对话框
                dialog.setCancelable(false);
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
                break;
            case R.id.button_order_personnalinfo_page_resident:
                intent=new Intent(this, OrderPage.class);
                startActivity(intent);
                break;
            case R.id.button_modifyinfo_personnalinfo_page_resident:
                intent=new Intent(this, InformationModificationPage.class);
                startActivity(intent);
                break;
            case R.id.button_logout_personnalinfo_page_resident:
                ActivityManagement.KillAllAcitivity();
                break;
            case R.id.button_shiftaccount_personnalinfo_page_resident:
                ActivityManagement.turnToLoginPage();
                break;
        }
    }

    protected void onDestroy() {
        ActivityManagement.KillOneActivity();
        super.onDestroy();
    }
}
